package com.fc.advbox.service.impl;

import com.fc.advbox.dao.*;
import com.fc.advbox.dto.investor.InvestorAcctDto;
import com.fc.advbox.entity.InvestorAcctEntity;
import com.fc.advbox.entity.InvestorAcctTxnEntity;
import com.fc.advbox.entity.InvestorAssetEntity;
import com.fc.advbox.entity.UserPointEntity;
import com.fc.advbox.service.IInvestorAcctService;
import com.fc.advbox.utils.Constant;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * InvestorAcctServiceImpl
 *
 * @author Gary.Jiang
 * @date 2018/6/26.16:54
 */
@Service
public class InvestorAcctServiceImpl extends BaseServiceImpl<InvestorAcctEntity> implements IInvestorAcctService {
    private static final Logger log = LoggerFactory.getLogger(InvestorAcctServiceImpl.class);

    @Autowired
    private InvestorAcctDao investorAcctDao;

    @Autowired
    private InvestorAssetDao investorAssetDao;

    @Autowired
    private UserPointDao pointDao;

    @Autowired
    private InvestorAcctTxnDao investorAcctTxnDao;

    @Autowired
    private UserDao userDao;

    @Override
    public BaseDao getDao() {
        return investorAcctDao;
    }

    @Override
    public BigDecimal queryTotalProfit(Map<String, Object> params) {
        return investorAcctDao.queryTotalProfit(params);
    }

    @Override
    public List<InvestorAcctEntity> queryInvestorAcctByUserId(Long userId){
        return investorAcctDao.queryByUserId(userId);
    }

    @Override
    public InvestorAcctEntity queryInvestorAcctByUserIdAndAcctId(Long userId, Long acctId){
        return investorAcctDao.queryByUserIdAndAcctId(userId, acctId);
    }

    public InvestorAcctDto queryInvestorProfitByUserId(Long userId){
        InvestorAcctDto dto = new InvestorAcctDto();
        Map params = new HashedMap();
        params.put("userId", userId);
        List<InvestorAssetEntity> assetList = investorAssetDao.queryList(params);
        dto.setAssetList(assetList);
        List<InvestorAcctEntity> acctList = investorAcctDao.queryByUserId(userId);
        //取第一个CASH账户
        InvestorAcctEntity acct = new InvestorAcctEntity();
        for(InvestorAcctEntity a: acctList){
            if(Constant.AcctType.CASH.getValue().equals(a.getType())){
                acct = a;
                break;
            }
        }
        dto.setInvestorAcct(acct);
        UserPointEntity upe = pointDao.queryByUserId(userId);
        if(upe != null){
            dto.setPoints(upe.getPoint());
        }else{
            dto.setPoints(0);
        }
        return dto;
    }

    @Override
    public List<InvestorAcctEntity> queryLimit(Integer offset, Integer num){
        return investorAcctDao.queryLimit(offset,num);
    }

    @Override
    @Transactional
    public void increase(Long userId, double amt){
        List<InvestorAcctEntity> acctList  = investorAcctDao.queryByUserId(userId);
        if(acctList == null){
            log.warn("No acct for investor:["+userId+"]");
            return;
        }
        //取第一个CASH账户
        InvestorAcctEntity acct = new InvestorAcctEntity();
        for(InvestorAcctEntity a: acctList){
            if(Constant.AcctType.CASH.getValue().equals(a.getType())){
                acct = a;
                break;
            }
        }
        acct.setBalance(acct.getBalance()==null?new BigDecimal(0):new BigDecimal(acct.getBalance().doubleValue()+amt));
        acct.setProfit(acct.getProfit()==null?new BigDecimal(0):new BigDecimal(acct.getProfit().doubleValue()+amt));
        investorAcctDao.update(acct);
        InvestorAcctTxnEntity txn = new InvestorAcctTxnEntity();
        txn.setAcctId(acct.getId());
        txn.setAmount(new BigDecimal(amt));
        txn.setBalance(acct.getBalance()!=null?acct.getBalance():new BigDecimal(0));
        txn.setCreateTime(new Date());
        txn.setReciprocalAccount("system");
        txn.setReciprocalName("system");
        txn.setTxnType(Constant.AcctTxnType.MONTHLY_INTEREST_SETTLE.getValue());
        investorAcctTxnDao.save(txn);
    }
}
